package com.lovo.csc.service.impl;

import com.lovo.csc.entity.Purchasenoe;
import com.lovo.csc.mapper.PurchasenoeMapper;
import com.lovo.csc.service.IPurchasenoeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.sscsys.dto.BuyPlanMQDto;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-13
 */
@Service
public class PurchasenoeServiceImpl extends ServiceImpl<PurchasenoeMapper, Purchasenoe> implements IPurchasenoeService {
    @Autowired
    IPurchasenoeService purchasenoeService;


    @RabbitListener(queues = "buyPlanQueue")
    public void savePurchasenoe(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, BuyPlanMQDto dto) throws IOException {
//        if (dto!=null){
        Purchasenoe purchasenoe = new Purchasenoe();
        purchasenoe.setGoodname(dto.getGoodsName());
        purchasenoe.setGoodsort(dto.getType());
        purchasenoe.setGoodinfo(dto.getSpecs());
        purchasenoe.setPurchasecode(dto.getCode());
        purchasenoe.setPurchasenum(dto.getNum());
        purchasenoe.setPurchaselstate("未审核");
        purchasenoe.setPurchasedate(String.valueOf(LocalDate.now()));
        purchasenoe.setStorkinfo(dto.getReason());
        purchasenoeService.save(purchasenoe);
        channel.basicAck(tag,false);
//        }
    }

    @Override
    public void updatePurchasenoeState(int PurchasenoeId, String purchaselstate) {
        baseMapper.updatePurchasenoeState(PurchasenoeId,purchaselstate);
    }
}
